<template>
  <q-page class="cc-admin">

    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">

        <template v-slot:top="">
          <div class="row no-wrap full-width ">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <!--  <table-top-btns label="应急训练和演习记录" modelName="trainingDrillRecords" :showAdd="true"
                :table="table" :selected="selected" :importing="importing" :exporting="exporting"
                :importExcelUrlFull="importExcelUrlFull" :columns="columns" @groupChange="groupChange"
                @add="add" @importExcel="importExcel" @exportExcel="exportExcel" @showConfirm="showConfirm"/> -->
            <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />

            </div>
          </div>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></q-btn>
            <!-- <q-btn
                                flat
                                round
                                dense
                                color="primary"
                                icon="mdi-content-copy"
                                @click="copy(props.row)"
                        >
                            <q-tooltip>复制</q-tooltip>
                        </q-btn> -->
            <btn-del label="应急训练和演习记录" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <!-- <div class="col-6">
                                                              <h5> 编号：</h5>
                                                                  <q-input outlined dense v-model="form.id"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 记录员：</h5>
                                                                  <q-input outlined dense v-model="form.recorder"  type="text"   />
                                                          </div> -->
            <div class=" col-6">
              <div class="dynamic-form-item ">
                <div v-show="true" class="dynamic-form-item-label">
                  <q-icon name="star" color="red" v-show="true" />
                  平台名称
                </div>
                <!-- <select-search :formData="form" formKey="shipId" dense debounce="500" hide-bottom-space outlined v-model="form.shipId" :options="ship_idOptions" type="select" @blur="ship_idBlur105" @input="ship_idInput105(form
                                            .shipId,form)" lazy-rules :rules="[]" clearable emit-value map-options /> -->
                <q-input outlined dense v-model="form.shipIdText" type="text" hide-bottom-space debounce="500" :rules="[requireTest]" />
              </div>
            </div>
            <div class="col-6" v-if="showForm[102]">
              <div class="dynamic-form-item ">
                <div v-show="true" class="dynamic-form-item-label ">
                  <q-icon name="star" color="red" v-show="true" />
                  日期
                </div>
                <input-date-picker dense debounce="500" hide-bottom-space outlined v-model="form.dates" type="date" :form="form" beginDate="" endDate="" @blur="datesBlur102" @input="datesInput102(form
                  .dates, form)" lazy-rules :rules="[requireTest]" :readonly="!editableForm[102]" />
              </div>
            </div>

            <!-- <div class="col-6">
                                    <div v-show="true" class="dynamic-form-item-label" style="width:100%;">
                                        <q-icon name="star" color="red" v-show="true" />
                                        种类
                                    </div>
                                    <select-search :formData="form" formKey="category" dense debounce="500" hide-bottom-space outlined :options="categoryOptions" v-model="form.category" type="select" @blur="categoryBlur107" @input="categoryInput107(form
                                        .category,form)" lazy-rules :rules="[requireTest]" clearable emit-value map-options />
                                    </div> -->
            <!-- <div class="col-6">
                                                              <h5> 地点：</h5>
                                                                  <q-input outlined dense v-model="form.area"  type="text"   />
                                                          </div> -->
            <div class="col-12">
              <h5> 参加人</h5>
              <q-input outlined dense v-model="form.attendants" type="textarea" />
            </div>
            <div class=" col-12">
              <div class="dynamic-form-item ">
                <div v-show="true" class="dynamic-form-item-label">
                  <q-icon name="star" color="red" v-show="true" />
                  应急演练内容
                </div>
                <!-- <select-search :formData="form" formKey="shipId" dense debounce="500" hide-bottom-space outlined v-model="form.shipId" :options="ship_idOptions" type="select" @blur="ship_idBlur105" @input="ship_idInput105(form
                                            .shipId,form)" lazy-rules :rules="[]" clearable emit-value map-options /> -->
                <q-input outlined dense v-model="form.walkthroughContent" type="textarea" :rules="[requireTest]" />
              </div>
            </div>
            <div class="col-12">
              <h5> 应急演练过程记录</h5>
              <q-input outlined dense v-model="form.exerciseRecord" type="textarea" />
            </div>
            <div class="column col-12">
              <input-upload v-show="true" title="附件上传" uploadType="" fileNumber="1000" :required="false" :readonly="false" :formId="formId" fieldId="110" :dataId="dataId" :imageUpload="true" uploadBtnlabel="上传附件" />
            </div>

            <!--                                                           <div class="col-6">
                                                              <h5> 船舶名称名称：</h5>
                                                                  <q-input outlined dense v-model="form.shipIdText"  type="text"   />
                                                          </div>

                                                            <div class="col-6">
                                                              <h5> 种类名称：</h5>
                                                                  <q-input outlined dense v-model="form.categoryText"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 状态：</h5>
                                                                  <q-input outlined dense v-model="form.status"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 预选id：</h5>
                                                                  <q-input outlined dense v-model="form.selfSelectId"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 审批节点：</h5>
                                                                  <q-input outlined dense v-model="form.node"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 创建人姓名：</h5>
                                                                  <q-input outlined dense v-model="form.createByName"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 更新人姓名：</h5>
                                                                  <q-input outlined dense v-model="form.updateByName"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 分类目录：</h5>
                                                                  <q-input outlined dense v-model="form.catalogId"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 分类目录名称：</h5>
                                                                  <q-input outlined dense v-model="form.catalogIdText"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 上级编号：</h5>
                                                                  <q-input outlined dense v-model="form.parentId"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 所属机构名称：</h5>
                                                                  <q-input outlined dense v-model="form.sysOrgName"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 部门：</h5>
                                                                  <q-input outlined dense v-model="form.sysDeptCode"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 部门名称：</h5>
                                                                  <q-input outlined dense v-model="form.sysDeptName"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 公司ID：</h5>
                                                                  <q-input outlined dense v-model="form.tenantId"  type="text"   />
                                                          </div>
                                                          <div class="col-6">
                                                              <h5> 船舶类型：</h5>
                                                                  <q-input outlined dense v-model="form.shipType"  type="text"   />
                                                          </div> -->
          </div>

        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import inputUpload from 'components/form/inputupload';

export default {
  name: 'EmergencyTrainingDrillRecords',
  mixins: [IndexMixin],
  components: {
    confirm,
    inputUpload
  },
  data() {
    return {
      categoryOptions: [{
        label: '船舶应急训练和演习',
        value: '1'
      }, {
        label: '岸基应急训练',
        value: '2'
      }],
      categoryOptionData: [],
      flowShowViewForm: undefined,
      emptyForm: {
        id: null,
        recorder: null,
        dates: null,
        shipId: '',
        shipIdText: null,
        category: '',
        categoryText: null,
        area: null,
        walkthroughContent: null,
        attendants: null,
        exerciseRecord: null
      },
      searchForm: {
        search_2: '',
        search_3: '',
        search_0: ''
      },
      showForm: {
        101: true,
        102: true,
        105: true,
        107: true,
        108: true,
        109: true,
        110: true,
        upload110: true,
        download110: true,
        del110: true
      },
      editableForm: {
        101: true,
        102: true,
        105: true,
        107: true,
        108: true,
        109: true
      },
      resetShowForm: {
        101: true,
        102: true,
        105: true,
        107: true,
        108: true,
        109: true,
        110: true,
        upload110: true,
        download110: true,
        del110: true
      },

      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        /* {
          name: 'id', align: 'left', label: '编号', field: 'id',
        },
        {
          name: 'recorder', align: 'left', label: '记录员', field: 'recorder',
        }, */

        {
          name: 'shipId', align: 'center', label: '平台名称', field: 'shipIdText',
        },
        {
          name: 'dates', align: 'center', label: '日期', field: 'dates',
        },
        /*  {
          name: 'category', align: 'left', label: '种类', field: 'category',
        },
        {
          name: 'area', align: 'left', label: '地点', field: 'area',
        }, */
        /* {
          name: 'attendants', align: 'left', label: '参加人', field: 'attendants',
        }, */
        {
          name: 'walkthroughContent', align: 'center', label: '应急演练内容', field: 'walkthroughContent',
        },
        /* {
          name: 'exerciseRecord', align: 'left', label: '应急演练过程记录', field: 'exerciseRecord',
        }, */
        /*  {
          name: 'shipIdText', align: 'left', label: '船舶名称名称', field: 'shipIdText',
        },
        {
          name: 'categoryText', align: 'left', label: '种类名称', field: 'categoryText',
        },
        {
          name: 'status', align: 'left', label: '状态', field: 'status',
        },
        {
          name: 'selfSelectId', align: 'left', label: '预选id', field: 'selfSelectId',
        },
        {
          name: 'node', align: 'left', label: '审批节点', field: 'node',
        },
        {
          name: 'createByName', align: 'left', label: '创建人姓名', field: 'createByName',
        },
        {
          name: 'updateByName', align: 'left', label: '更新人姓名', field: 'updateByName',
        },
        {
          name: 'catalogId', align: 'left', label: '分类目录', field: 'catalogId',
        },
        {
          name: 'catalogIdText', align: 'left', label: '分类目录名称', field: 'catalogIdText',
        },
        {
          name: 'parentId', align: 'left', label: '上级编号', field: 'parentId',
        },
        {
          name: 'sysOrgName', align: 'left', label: '所属机构名称', field: 'sysOrgName',
        },
        {
          name: 'sysDeptCode', align: 'left', label: '部门', field: 'sysDeptCode',
        },
        {
          name: 'sysDeptName', align: 'left', label: '部门名称', field: 'sysDeptName',
        },
        {
          name: 'tenantId', align: 'left', label: '公司ID', field: 'tenantId',
        },
        {
          name: 'shipType', align: 'left', label: '船舶类型', field: 'shipType',
        }, */
        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      form: {
        id: null,
        recorder: null,
        dates: null,
        shipId: '',
        shipIdText: null,
        category: '',
        categoryText: null,
        area: null,
        walkthroughContent: null,
        attendants: null,
        exerciseRecord: null
      },
      ship_idOptionData: [],
      ship_idOptions: [],
      search_0Options: [],
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/emergency/trainingDrillRecords/list',
        add: '/emergency/trainingDrillRecords/add',
        edit: '/emergency/trainingDrillRecords/edit',
        copy:
          '/emergency/trainingDrillRecords/copy',
        delete: '/emergency/trainingDrillRecords/delete',
        deleteBatch: '/emergency/trainingDrillRecords/deleteBatch',
        exportXlsUrl: '/emergency/trainingDrillRecords/exportXls',
        importExcelUrl: '/emergency/trainingDrillRecords/importExcel',
      },
    };
  },
  methods: {
    requireTest(val) {
      return val === 0 || (val && ((`${val}`).length > 0)) || '这是必填字段';
    },
    categoryBlur107(evt, editAfter) {

    },
    categoryInput107(v, row, editAfter) {
      this.showForm[105] = !((this.form.category === '2'));

      // 这里给名称字段赋值
      if (v instanceof Array) {
        const nameArray = [];
        if (this.form) {
          this.form.categoryText = '';
          v.forEach((id) => {
            const opt = this.categoryOptions.find((o) => o.value === id);
            if (opt) {
              nameArray.push(opt.label);
            }
            this.form.categoryText = nameArray.join(',');
          });
        }
      } else {
        const opt = this.categoryOptions.find((o) => o.value === v);
        if (opt) {
          this.form.categoryText = opt.label;
        }
        if (!v) {
          this.form.categoryText = '';
        }
      }
    },
    ship_idBlur105(evt, editAfter) {

    },
    ship_idInput105(v, row, editAfter) {
      // 这里给名称字段赋值
      if (v instanceof Array) {
        const nameArray = [];
        if (this.form) {
          this.form.shipIdText = '';
          v.forEach((id) => {
            const opt = this.ship_idOptions.find((o) => o.value === id);
            if (opt) {
              nameArray.push(opt.label);
            }
            this.form.shipIdText = nameArray.join(',');
          });
        }
      } else {
        const opt = this.ship_idOptions.find((o) => o.value === v);
        if (opt) {
          this.form.shipIdText = opt.label;
        }
        if (!v) {
          this.form.shipIdText = '';
        }
      }
    },
    addBefore() {
      this.form = {
        dynFormId: this.formId,
        parentFormId: this.parentFormId,
        parentId: this.parentId,
        catalog_id: this.catalog,
        ...this.emptyForm
      };

      if (this.form.shipId) {
        this.shipIdInput105(this.form.shipId);
      }
      if (this.form.category) {
        this.categoryInput107(this.form.category);
      }

      return true;
    },
    editAfter() {
      // 只有编辑触发 为 true才进行触发
      if (this.form.shipId) {
        this.ship_idInput105(this.form.shipId, {}, true);
      }
      if (this.form.category) {
        this.categoryInput107(this.form.category, {}, true);
      }
    },
    getDictLabel,
    initDict() {
      this.$axios.get('/sys/ship/list', this.form).then(({
        result
      }) => {
        console.log(result);
        console.log(result.records);
        if (result.records && result.records.length > 0) {
          this.ship_idOptionData = result.records;
          this.ship_idOptions = result.records.map((m) => ({
            label: m.shipName,
            value: m.id
          }));
          console.log(result.records[0].shipName);
          this.search_0Options = result.records.map((m) => ({
            label: m.shipName,
            value: m.id
          }));

          if (result.records.length === 1) {
            this.form.shipId = this.ship_idOptions[0].value;
            this.form.shipIdText = this.ship_idOptions[0].label;
          }
        } else {
          this.ship_idOptionData = [];
          this.ship_idOptions = [];
        }
      });
    },

  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus">

</style>
